<template>
    <view>
        <scroll-view :scroll-y="true" :scroll-x="false" class="scroll" @scrolltolower="bindlower">
            <view class="wuge" v-if="allCourses == '' || allCourses.length == 0">没有任何课程信息</view>
            <view class="list" v-for="(item, index) in allCourses" :key="index">
                <view class="item" @tap="toDetail" :data-id="item.id">
                    <view class="image" @tap="toDetail" :data-id="item.id">
                        <image :src="item.videoCover" />
                    </view>
                    <view class="content">
                        <text class="h1">{{ item.courseTitle }}</text>
                        <text class="p">{{ item.courseContent }}</text>
                        <text class="p myp">{{ item.courseLabelNames }}</text>
                    </view>
                </view>
            </view>
        </scroll-view>
        <view style="margin-top:102%">
          <button type="default" @click="share">分享</button>
          <uniPopup ref="popup" type="share">
            <uniShare title="分享到" @select="select"></uniShare>
          </uniPopup>
        </view>
    </view>
</template>

<script>
var config = require('../../config.js');
var http = require('../../utils/httpHelper.js');
import uniPopup from "@/components/uni-popup/uni-popup.vue";
import uniShare from "@/components/uni-popup-share-wx/uni-popup-share-wx.vue";
export default {
    components: {
			uniPopup,
			uniShare
		},
    data() {
        return {
            userInfo: {},
            isLogin: false,
            teacherId: 0,
            teacher: {},
            allCourses: [],
            sumPrice: 0,
            timer: {},
            coursePage: {
                current: 1,
                pageSize: 12,
                total: 0
            }
        };
    },
    onLoad: function (options) {      
        var that = this;
        let userInfo = uni.getStorageSync('user');
        if (userInfo != null && userInfo != undefined && userInfo != {} && userInfo != '') {
            that.setData({
                isLogin: true,
                userInfo: userInfo
            });
        } else {
            uni.setStorageSync('inviteId', options.inviteId);
        }
        uni.setNavigationBarTitle({
            title: options.teacherName + '-课程展示'
        });
        var teacherId = options.teacherId;
        that.getTeacherInfo(teacherId, function (teacher) {
            that.setData({
                teacher: teacher
            });
        });
        that.getCourses(teacherId, function (list) {
            that.setData({
                teacherId: teacherId,
                allCourses: list
            });
        });
    },
    onShow: function (e) {        
        let userInfo = uni.getStorageSync('user');
        if (userInfo != null && userInfo != undefined && userInfo != {} && userInfo != '') {
            this.setData({
                isLogin: true,
                userInfo: userInfo
            });
        }
    },
    methods: {
        getMoreData() {
            var that = this;
            that.coursePage.current++;
            that.getCourses(function (list) {
                let yList = that.allCourses;
                yList = yList.concat(list);
                that.setData({
                    allCourses: yList
                });
            });
        },

        getCourses: function (teacherId, callback) {
            var that = this;
            http.httpPost(
                config.clubApi.getCourseList,
                {
                    pageNo: that.coursePage.current,
                    pageSize: that.coursePage.pageSize,
                    teacherId: teacherId
                },
                function (res) {
                    if (res.code == '200') {
                        var list = res.data.pageData;
                        if (typeof callback == 'function') {
                            callback(list);
                        }
                    }
                }
            );
        },

        toDetail: function (e) {
            var id = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: '../course/detail/index?id=' + id
            });
        },

        getTeacherInfo: function (teacherId, callback) {
            var data = {
                id: teacherId
            };
            http.httpGet(config.clubApi.getTeacherInfo, data, function (res) {
                if (res.code == '200') {
                    var teacher = res.data;
                    if (typeof callback == 'function') {
                        callback(teacher);
                    }
                }
            });
        },

        //处理分页
        bindlower: function (e) {
          if(this.timer != null){
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(()=>{
            this.getMoreData();
          },500)
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
